package com.booth.manage.controllers;

import java.io.File;
import java.io.PrintWriter;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Random;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.booth.base.controllers.BaseController;
/**
 * html编辑器保存文件
 */
public class CKEditorController extends BaseController {
	private String CKEditorFuncNum;
	private String newName;
	private String str;
	public void imageUpload()
	{
		CKEditorFuncNum = getPara("CKEditorFuncNum");
		String savePath = this.creatSavePath("");
//		System.out.println("savePath=:"+savePath);
		com.jfinal.upload.UploadFile file = getFile("upload",savePath);
//		String ss = getPara("CKEditor");
//		System.out.println("ss=:"+ss);
		/*String f1 = file.getContentType();//得到上传文件的类型
		String f3 = file.getOriginalFileName();//得到文件原始名称
		String f4 = file.getParameterName();//得到上传的参数，此处为upload
		String f5 = file.getSaveDirectory();//得到文件的保存路径
		String f6 = file.getFileName();//得到文件保存后的名称
		*/
		File f2 = file.getFile();//得到文件保存的完整路径
//		System.out.println("f2=:"+f2);
		File f = new File(f2.toString());
		if(f.exists())
		{
			String name = f.getName();
			newName = creatSaveFileName(name);//获取新的名称
			File df = new File(savePath,newName);
			f.renameTo(df);
		}
		String url = creatHeaderUrl();
		String retUrl = (url+"/"+str+"/"+newName).replace(File.separator,"/");
		System.out.println("retUrl=:"+retUrl);
		
		HttpServletResponse response = getResponse();
		response.setContentType("text/html; charset=UTF-8");
		response.setHeader("Cache-Control", "no-cache");
		try {
			PrintWriter w = response.getWriter();
			w.write("<script type='text/javascript'>window.parent.CKEDITOR.tools.callFunction("+this.CKEditorFuncNum+", '" +retUrl+"', '');</script>");
			w.flush();
			w.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	/***创建访问的前缀***/
	private String creatHeaderUrl()
	{
		HttpServletRequest request = getRequest();
		String scheme = request.getScheme();//http
		String serverName = request.getServerName();//localhost
		int serverPort = request.getServerPort();//端口号：8080
		String contextPath = request.getContextPath();//项目放置的web 例如根目录/ 或者自己定义的目录
		String url = scheme+"://"+serverName+":"+serverPort+contextPath;
		String headerUrl = url.replace(File.separator, "/");
//		System.out.println("headerUrl=:"+headerUrl);
		return headerUrl;
		
	}
	/***创建保存的路径***/
	private String creatSavePath(String myPath)
	{
		HttpServletRequest request = getRequest();
		String realPath = request.getRealPath("/");
		System.out.println("realPath=:"+realPath);
		SimpleDateFormat year = new SimpleDateFormat("yyyy");//获取当前系统年份
		SimpleDateFormat month = new SimpleDateFormat("MM");//获取当前系统月份
		Date curDate = new Date(System.currentTimeMillis());
		str = year.format(curDate)+File.separator+month.format(curDate);
		String mySavePath = null;
		if(myPath != null && !myPath.equals(""))
		{
			str = myPath+"/"+year.format(curDate)+File.separator+month.format(curDate);
			mySavePath = str;
//			System.out.println("mySavePath=:"+mySavePath);
		}
		else
			mySavePath = str;
		String savePath = (realPath+'\\'+mySavePath).replace(File.separator, "/");
//		System.out.println("savePath=:"+savePath);
		return savePath;
	}
	/***创建保存的文件名***/
	public String creatSaveFileName(String name)
	{
		String newName = null;
		int index = name.indexOf(".");
		String sub = name.substring(index+1);//后缀名
		DateFormat dft = new SimpleDateFormat("yyMMddHHmmssSSS");
		String dftDate = dft.format(new Date());
		int random = new Random().nextInt(10000);
		newName = dftDate + random + "." + sub;
		return newName;
		
	}
}
